﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using HPAGMRestAPIWrapper;
using AGMLite.Windows;
using System.Collections;
using AGMLite.BaseClass;
using System.Windows;

namespace AGMLite.ViewModelClass
{
    public class ProjectsViewModel : ViewModelBase
    {
        #region Properties
        public ObservableCollectionEx<ProjectViewModel> ProjectCollection { get; set; }
        #endregion

        #region Private members
        private RelayCommand<object> addCommand;
        private RelayCommand<object> editCommand;
        private RelayCommand<object> deleteCommand;
        private RelayCommand<object> connectCommand;
        #endregion

        public ProjectsViewModel()
        {
        }

        public void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
        {
        }

        #region Commands
        public System.Windows.Input.ICommand AddCommand
        {
            get
            {
                if (addCommand == null)
                {
                    addCommand = new RelayCommand<object>(x => this.Add(), x => this.CanAdd());
                }
                return addCommand;
            }
        }

        public System.Windows.Input.ICommand EditCommand
        {
            get
            {
                if (editCommand == null)
                {
                    editCommand = new RelayCommand<object>(x => this.Edit(x), x => this.CanEdit(x));
                }
                return editCommand;
            }
        }

        public System.Windows.Input.ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new RelayCommand<object>(x => this.Delete(x), x => this.CanDelete(x));
                }
                return deleteCommand;
            }
        }

        public System.Windows.Input.ICommand ConnectCommand
        {
            get
            {
                if (connectCommand == null)
                {
                    connectCommand = new RelayCommand<object>(x => this.Connect(x), x => this.CanConnect(x));
                }
                return connectCommand;
            }
        }
        #endregion

        #region Implementation
        public void Add()
        {      
            AGMProject newProject = new AGMProject()
            {
                Name = "New Project",
                Server = "https://",
                ItemCollection = new ObservableCollectionEx<AGMItem>(),
                EntityFields = new Dictionary<string, List<AGMField>>(),
                Icon = "",
            };
            ProjectViewModel newPVM = new ProjectViewModel(newProject);
            OpenProjectEditWindow(newPVM);
            if (!string.IsNullOrEmpty(newProject.GUID))
            {
                ProjectCollection.Add(newPVM);
                newProject.StartTimer();
                newProject.Connect();
            }
        }

        public void Edit(object obj)
        {
            if (obj != null)
            {
                ProjectViewModel selectedProjectVM = obj as ProjectViewModel;

                selectedProjectVM.CloneProject();
                OpenProjectEditWindow(selectedProjectVM);

                if (selectedProjectVM.IsRequireReconnect)
                    selectedProjectVM.Project.Connect();
            }
        }

        public void Delete(object obj)
        {
            if (obj != null)
            {
                ProjectViewModel selectedProjectVM = obj as ProjectViewModel;

                string Message = null;

                if (selectedProjectVM.Project.ItemCollection.Count > 0)
                {
                    Message = "The project with some monitor items, delete the project will remove all monitor items permanently. \r\n Are you sure to delete the project?";
                }
                else
                {
                    Message = "Are you sure to delete the project?";
                }

                MessageBoxResult result = MessageBox.Show(Message, "Delete Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    ProjectCollection.Remove(selectedProjectVM);
                    selectedProjectVM.Project.Save(true);
                }
            }
        }

        public void Connect(object obj)
        {
            if (obj != null)
            {
                ProjectViewModel selectedProjectVM = obj as ProjectViewModel;
                selectedProjectVM.Project.Connect();
            }
        }

        public bool CanAdd()
        {
            return true;
        }

        public bool CanEdit(object obj)
        {
            if (obj != null)
                return true;
            return false;
        }

        public bool CanDelete(object obj)
        {
            if (obj != null)
                return true;
            return true;
        }

        public bool CanConnect(object obj)
        {
            if (obj != null)
                return true;
            return false;
        }

        #endregion

        #region Private Methods
        private ProjectViewModel OpenProjectEditWindow(ProjectViewModel projectVM)
        {
            projectVM.Project.IsEditing = true;
            ProjectEditWindow projectEditWindow = new ProjectEditWindow();
            projectEditWindow.DataContext = projectVM;
            projectEditWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            projectEditWindow.ShowDialog();
            projectVM.Project.IsEditing = false;

            return projectVM;
        }
        #endregion

    }
}
